#!/bin/bash

set -x

work_path=$(dirname $(realpath $0)) 

trap "${work_path}/scripts/umount.sh ${ramfs};exit" SIGHUP SIGINT SIGTERM  

if [ $# -ne 1 ]; then
	echo "It takes one parameters"
	exit
fi

ramfs=$1

if [ ! -d ${ramfs} ]; then
	echo "Please enter the correct ramfs path"
	exit
fi

openssh=${work_path}/config/app/openssh

if [ ! -d ${openssh} ]; then
	exit
fi
set +x
while read line
do
	tline=$(eval echo ${line})
	if [ -z "$tline" ]; then
		continue
	fi
	rm -fr ${ramfs}/${line}
done < ${work_path}/config/package/remove-file.list
set -x

sed -i 's/\(^\:colorscheme.*\)/"\1/g' ${ramfs}/etc/vim/vimrc

mkdir -p ${ramfs}/etc
mkdir -p ${ramfs}/usr/{bin,sbin}
cp -a ${openssh}/etc/ssh ${ramfs}/etc/
cp -a ${openssh}/usr/bin/* ${ramfs}/usr/bin/
cp -a ${openssh}/usr/sbin/* ${ramfs}/usr/sbin/

cp -a ${work_path}/config/hook ${ramfs}/tmp/

ls ${ramfs}/tmp/ && ls ${ramfs}/tmp/hook/
chroot ${ramfs} bash -c 'for hook in $(find /tmp/hook/  -type f); do [ -x $hook ] && bash -x "$hook"; done'




